package org.wzh.three.jvm.oom;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * 本机直接内存溢出
 * @author ewanhiz
 * VM Args: -Xmx20m -XX:MaxDirectMemorySize=10m
 *
 */
public class DirectMemoryOOM {

    private static final int _1MB = 1024 * 1024;

    public static void main(String[] args) throws IllegalAccessException {
        Field unsafeField = Unsafe.class.getDeclaredFields()[0];
        unsafeField.setAccessible(true);
        Unsafe unsafe = (Unsafe) unsafeField.get(null);
        int count = 0;
        while(true) {
            System.out.println(count++);
            unsafe.allocateMemory(_1MB);
        }
    }

}
